var express = require('express');
var router = express.Router();
var connection = require('../database/connection');

router.post('/login',async function(req,res){
    let email = req.body.email;
    let password = req.body.password;
    let username = '';
    let pwd = '';
    let identity = '';
    let sql = "select username,password,identity from user where email=?";
    let arg = [];
    arg.push(email);
    let db_res = await connection.dbQuery(sql,arg);
    if(db_res.err===-1){
        console.log("数据库连接失败");
        res.send({'errcode':4});
        return;
    }else if(db_res.err===-2){
        console.log("操作失败");
        res.send({'errcode':4});
        return;
    }else{
        if(db_res.result.length===0){
            res.send({'errcode':1});
            res.end();
            return;
        }
        username = db_res.result[0].username;
        pwd = db_res.result[0].password;
        identity = db_res.result[0].identity;
        if(pwd!==password){
            console.log("2");
            res.send({'errcode':2});
            res.end();
            return;
        }
        req.session.username = username;
        req.session.identity = identity;
        req.session.email=email;
        res.send({'errcode':0,'nickname':username,'identity':identity});
        res.end();
    }
});

router.post('/register',async function(req,res){
    console.log(req.body);
    let email = req.body.email;
    let username = req.body.username;
    let password = req.body.password;
    let verificationcode = req.body.verificationcode;
    let identity = req.body.identity;
    let pwd = '';

    let sql = "select code from verificationcode where email=?";
    let arg = [];
    arg.push(email);
    let flag = false;
    let db_res = await connection.dbQuery(sql,arg);
    if(db_res.err===-1){
        console.log("数据库连接失败");
        res.send({'errcode':4});
        return;
    }else if(db_res.err===-2){
        console.log("操作失败");
        res.send({'errcode':4});
        return;
    }else{
        if(db_res.result.length===0){
            console.log(db_res.result);
            res.send({'errcode':2});
            res.end();
            flag = true;
            return;
        }
        if(db_res.result[0].code!==verificationcode){
            console.log(db_res.result[0].code);
            console.log(verificationcode);
            res.send({'errcode':3});
            res.end();
            return;
        }
    }

    sql = "select id from user where email=?";
    arg = [];
    arg.push(email);
    db_res = await connection.dbQuery(sql,arg);
    if(db_res.err===-1){
        console.log("数据库连接失败");
        res.send({'errcode':4});
        return;
    }else if(db_res.err===-2){
        console.log("操作失败");
        res.send({'errcode':4});
        return;
    }else{
        if(db_res.result.length!==0){
            res.send({'errcode':1});
            res.end();
            return;
        }
    }
    sql = "select id from user where username=?";
    arg = [];
    arg.push(username);
    db_res = await connection.dbQuery(sql,arg);
    if(db_res.err===0){
        if(db_res.result.length!==0){
            res.send({'errcode':5});
            res.end();
            return;
        }
    }
    sql = "insert into user(username,password,email,identity) values(?,?,?,?)";
    arg = [];
    arg.push(username);
    arg.push(password);
    arg.push(email);
    arg.push(identity);
    db_res = await connection.dbQuery(sql,arg);
    if(db_res.err === 0){
        req.session.username = username;
        req.session.email = email;
    }
    if(db_res.err===-1){
        console.log("数据库连接失败");
        res.send({'errcode':4});
        return;
    }else if(db_res.err===-2){
        console.log("操作失败");
        res.send({'errcode':4});
        return;
    }else{
        //req.session.username = req.body.username;
        res.send({'errcode':0});
        res.end();
    }
});
router.post('/check',async function(req,res){
    let email = req.body.email;
    let sql = "select id from user where email = ?";
    let arg = [];
    arg.push(email);
    let db_res = await connection.dbQuery(sql,arg);
    if(db_res.err===0){
        if(db_res.result.length===0){
            res.send({'result':false});
            res.end();
        }else{
            res.send({'result':true});
            res.end();
        }
    }
})

router.post('/logout',function(req,res){
    delete req.session.username;
    delete req.session.id;
    if(!req.session.username){
        res.send({'errcode':0});//成功
        res.end();
        return;
    }
    res.send({'errcode':1});//失败
    res.end();
})

router.post('/login_check',function(req,res){
    if(!req.session.username){
        res.send({'errcode':1});
        res.end();
        return;
    }
    res.send({'errcode':0,'username':req.session.username,'identity':req.session.identity});
    res.end();
})


module.exports = router;